/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wanhai.itm.lov;

 
import com.wanhai.data.tool.ConnectionBuilder;
import com.wanhai.data.tool.DataSet;
import com.wanhai.itm.util.DataSetService;
import com.wanhai.itm.util.VoDataService;
import com.wanhai.vo.was.WAS2002VO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.zkoss.bind.Binder;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
 

public class LovVM{
     
    private Map args;
    private WAS2002VO lovDef;
    private DataSetService dsService = new DataSetService();
    private VoDataService<WAS2002VO> voService = new VoDataService<WAS2002VO>();
    
    private String isMultiple;
    private String strQueryValue;
    private List<String> columnNameList = new ArrayList<String>();
    private List<Map> lovItems;
    private List<Map> selectedItems;
    
    int nMaxRecords = 50;
    private StringBuilder strSQL = new StringBuilder();
    private String strJNDIName = "java:/OraWhl2DS";
    
    private Connection con;
    private PreparedStatement oPST;
    private ResultSet oRS;
    
    @Init
    public void init(@ContextParam(ContextType.VIEW) Component view){
        this.args = (HashMap) Executions.getCurrent().getArg();
        
        this.setIsMultiple(args.get("isMultiple").toString());
        
        WAS2002VO cond = new WAS2002VO();
        cond.setLOV_NAME(args.get("lovName").toString());
        
        try{
            List<WAS2002VO> voList = this.voService.find(cond);
            lovDef = voList.get(0);
            
            strSQL.append(" select * from ( ");
            strSQL.append(lovDef.getLOV_SQL());
            strSQL.append(" where "+lovDef.getQUERY_COLUMN_NAME()+" like ? "); 
            strSQL.append((!lovDef.getEXTRA_CONDITION().equals("")?" and ":"") +lovDef.getEXTRA_CONDITION());
            strSQL.append(" ) where rownum <"+String.valueOf(nMaxRecords+2));
            
            System.out.println("sql = "+strSQL);
            
            con = ConnectionBuilder.getConnection(strJNDIName);
            oPST = con.prepareStatement(strSQL.toString());
            oPST.setString(1, this.getStrQueryValue());
            oRS = oPST.executeQuery();
            ResultSetMetaData oMeta = oRS.getMetaData();
            int nColumnCount = oMeta.getColumnCount();
            
            for(int i=1 ; i<=nColumnCount ; i++){
                this.getColumnNameList().add(oMeta.getColumnName(i));
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(oRS != null){
                    oRS.close();
                    oRS = null;
                }
                if(oPST != null){
                    oPST.close();
                    oPST = null;
                }
                if(con != null){
                    con.close();
                    con = null;
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
        
//        System.out.println("init view = " +args.get("bindField"));
//        this.setBindField(bindField);
//        System.out.println("view = "+Path.getComponent("/win1").getId());
        
//        bindFieldComp = (Textbox) view.getParent().getFellow(args.get("bindField").toString());
//        bindFieldComp = (Textbox) view.getParent().getParent().getFellow(this.getBindField());
//        if(!Strings.isBlank(bindFieldComp.getValue())){
//            cond.setOFFICE_CODE(bindFieldComp.getValue());
//            query();
//        }
    }
//    @Init
//    public void init(@ExecutionArgParam("bindField") String bindField, @ContextParam(ContextType.VIEW) Component view){
//        System.out.println("init");
//        this.setBindField(bindField);
////        System.out.println("view = "+Path.getComponent("/win1").getId());
//        
//        bindFieldComp = (Textbox) view.getParent().getFellow(this.getBindField());
////        bindFieldComp = (Textbox) view.getParent().getParent().getFellow(this.getBindField());
//        if(!Strings.isBlank(bindFieldComp.getValue())){
//            cond.setOFFICE_CODE(bindFieldComp.getValue());
//            query();
//        }
//    }
    

    @Command("lovQuery")
    @NotifyChange("lovItems")
    public void query(){
        try{
            ArrayList condList = new ArrayList();
            condList.add(this.getStrQueryValue());
            
            DataSet ds = dsService.getMISCData(strSQL.toString(), condList);
            
            List<Map> rowMap = new ArrayList<Map>();
            for(int i=0 ; i<ds.getSize() ; i++){
                rowMap.add(ds.getRowValue(i));
            }
            
            this.setLovItems(rowMap);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    
    @Command
    public void choice(@ContextParam(ContextType.VIEW) Component view){
        Binder binder = (Binder) view.getParent().getAttribute("binder");
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("lovRtnList", this.selectedItems);
        
        binder.postCommand(args.get("callMethod").toString(),params);
//        Binder bind = (Binder) view.getParent().getParent().getAttribute("binder");
//        System.out.println("parent = "+ bind.getViewModel().getClass());
//        System.out.println("parent = "+ view.getId());
//        System.out.println("parent = "+ view.getParent().getParent().getFellow("OFFICE_CODE").getId());
        
//        bindFieldComp.setValue(this.getSelected().getOFFICE_CODE());
        view.detach(); 
//        this.getBindField().setOFFICE_CODE(this.getSelected().getOFFICE_CODE());
//        BindUtils.postNotifyChange(null, null, bind.getViewModel(), propertyRefresh);
//        view.setVisible(false);
    }
    


    //<editor-fold defaultstate="collapsed" desc="getter, setter">
    
    public WAS2002VO getLovDef() {
        return lovDef;
    }

    public void setLovDef(WAS2002VO lovDef) {
        this.lovDef = lovDef;
    }
    
    public String getStrQueryValue() {
        return strQueryValue;
    }

    public void setStrQueryValue(String strQueryValue) {
        this.strQueryValue = strQueryValue;
    }
    
    public List<String> getColumnNameList() {
        return columnNameList;
    }

    public void setColumnNameList(List<String> columnNameList) {
        this.columnNameList = columnNameList;
    }
    
    public List<Map> getLovItems() {
        return lovItems;
    }

    public void setLovItems(List<Map> lovItems) {
        this.lovItems = lovItems;
    }
    
    public List<Map> getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(List<Map> selectedItems) {
        this.selectedItems = selectedItems;
    }
    
    public String getIsMultiple() {
        return isMultiple;
    }

    public void setIsMultiple(String isMultiple) {
        this.isMultiple = isMultiple;
    }
    //</editor-fold>












}
